package com.carlinfo.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.users.dao.IProdDao;
import com.carlinfo.users.pojo.AProd;
import com.carlinfo.users.service.IProdService;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;


/**
 * @author Gonsen
 * service的实现类
 */
@Service("prodService")
public class ProdServiceImpl implements IProdService {

	@Resource
	private IProdDao prodDao;
	
	/**
	 * 保存一条数据
	 */
	@Override
	public JSONObject saveOneService(AProd prod) {
		JSONObject resultJSON = new JSONObject();
		int res = this.prodDao.saveOne(prod);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", prod.getId() + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

	/**
	 * 查询一条数据信息
	 */
	@Override
	public AProd findOneProdService(Map<String, Object> conMap) {
		return this.prodDao.findOne(conMap);
	}

	/**
	 * 查询多条数据信息
	 */
	@Override
	public List<AProd> findCondProdList(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			Page page = PageMethod.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());//当前页  每页多少条
			List<AProd> prodList = this.prodDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return prodList;
		}
		return this.prodDao.findCondList(condMap);
	}
	
	/**
	 * 更新一条数据
	 */
	@Override
	public JSONObject updateOneService(AProd prod) {
		JSONObject resultJSON = new JSONObject();
		int res = this.prodDao.updateOne(prod);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", prod.getId() + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

	/**
	 * 删除一条数据
	 */
	@Override
	public JSONObject deleteOneService(Map<String, Object> condMap) {
		JSONObject resultJSON = new JSONObject();
		int res = this.prodDao.deleteOne(condMap);
		
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("dffect", res + "");
			dataJSON.put("id", condMap.get("id") + "");
			/*把所需数据存放到resultJSON中*/
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			
			resultJSON.put("code", "-1");
			resultJSON.put("info", "失败"	);
		}
		return resultJSON;
	}

}
